package com.ruoyi.app.utils

import android.content.Context
import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey

class LocalStorageUtils {
    companion object {
        @JvmStatic var sharedPreferences : SharedPreferences? = null

        fun setKeyValue(key:String,value:String){
            sharedPreferences?.edit()
                ?.putString(key, value)
                ?.putBoolean("dark_mode", true)
                ?.apply()
        }

        fun getValue(key:String): String {
            return sharedPreferences?.getString(key, "") ?:""
        }

        fun getUserName():String{
            return getValue("username")
        }

        fun setUserName(value:String){
            sharedPreferences?.edit()
                ?.putString("username", value)
                ?.putBoolean("dark_mode", true)
                ?.apply()
        }

        fun setPreference(context: Context) {
            val masterKey = MasterKey.Builder(context)
                .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
                .build()
            val preference = EncryptedSharedPreferences.create(
                context,
                "user_settings",
                masterKey,
                EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
            )
            sharedPreferences = preference
        }
    }
}